package com.stellar.circle.domain.sIdle.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.stellar.circle.basic.entity.SIdle;
import com.stellar.circle.basic.service.SIdleService;
import com.stellar.circle.constants.ErrorCodeConstants;
import com.stellar.circle.controller.admin.sIdle.vo.SIdleRequestPageVO;
import com.stellar.circle.controller.admin.sIdle.vo.SIdleRequestVO;
import com.stellar.circle.controller.admin.sIdle.vo.SIdleResponseVO;
import com.stellar.circle.convert.SIdleConvert;
import com.stellar.circle.domain.sIdle.SIdleDomainService;
import com.stellar.web.core.bean.PageResult;
import com.stellar.web.core.enums.IsDeletedFlagEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.stellar.circle.constants.CircleConstants.*;
import static com.stellar.core.util.ServiceExceptionUtil.exception;

/**
 * 闲置物品交易表领域服务实现
 *
 * @author Jqchan
 * @version 1.0
 * @date 2025-06-07 20:13:40
 */
@Service
public class SIdleDomainServiceImpl implements SIdleDomainService {

    @Resource
    private SIdleService sIdleService;

    @Override
    public PageResult<SIdleResponseVO> page(SIdleRequestPageVO requestPageVO) {
        PageResult<SIdleResponseVO> pageResult = new PageResult<>();
        SIdle queryEntity = SIdleConvert.INSTANCE.convert(requestPageVO);
        queryEntity.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());

        List<SIdle> entities = sIdleService.page(queryEntity,
                requestPageVO.getStart(), requestPageVO.getPageSize());

        if (entities.isEmpty()) {
            return pageResult;
        }

        Long count = sIdleService.count(queryEntity);
        List<SIdleResponseVO> vos = SIdleConvert.INSTANCE.convertEntityToRespList(entities);

        for (SIdleResponseVO vo : vos) {
            // 处理图片
            if (StringUtils.isNotBlank(vo.getImages())) {
                vo.setIdleImages(JSONUtil.toList(vo.getImages(), String.class));
            }
        }

        pageResult.setResult(vos);
        pageResult.setTotal(count.intValue());
        return pageResult;
    }

    @Override
    public Boolean update(SIdleRequestVO requestVO) {
        SIdle entity = SIdleConvert.INSTANCE.convert(requestVO);
        if (entity != null && entity.getId() != null && CollectionUtil.isNotEmpty(requestVO.getIdleImages())) {
            entity.setImages(JSONUtil.toJsonStr(requestVO.getIdleImages()));
        }
        return sIdleService.update(entity) != 0;
    }

    @Override
    public Boolean delete(Long id) {
        SIdle entity = new SIdle();
        entity.setId(id);
        entity.setIsDeleted(IsDeletedFlagEnum.DELETED.getVal());
        return sIdleService.update(entity) != 0;
    }

    @Override
    public Boolean insert(SIdleRequestVO requestVO) {
        SIdle entity = SIdleConvert.INSTANCE.convert(requestVO);
        entity.setViewCount(0);
        // TODO 对帖子内容进行审核
        entity.setIdleStatus(SIDLE_STATUS_PUBLISHED);
        if (CollectionUtil.isNotEmpty(requestVO.getIdleImages())) {
            entity.setImages(JSONUtil.toJsonStr(requestVO.getIdleImages()));
        }
        return sIdleService.insert(entity) != 0;
    }

    @Override
    public SIdleResponseVO get(Long id) {
        SIdle entity = sIdleService.queryById(id);
        return SIdleConvert.INSTANCE.convert(entity);
    }

    @Override
    public Boolean complete(Long id) {
        SIdle sIdle = sIdleService.queryById(id);
        if (sIdle != null) {
            if (SIDLE_STATUS_COMPLETED.equals(sIdle.getIdleStatus())) {
                return true;
            }
            sIdle.setIdleStatus(SIDLE_STATUS_COMPLETED);
            sIdle.setCompleteTime(new Date());
            return sIdleService.update(sIdle) != 0;
        }
        return false;
    }

    @Override
    public Boolean close(Long id) {
        SIdle sIdle = sIdleService.queryById(id);
        if (sIdle != null) {
            if (SIDLE_STATUS_COMPLETED.equals(sIdle.getIdleStatus())) {
                throw exception(ErrorCodeConstants.CIRCLE_SIDLE_COMPLETED);
            }
            sIdle.setIdleStatus(SIDLE_STATUS_CLOSED);
            return sIdleService.update(sIdle) != 0;
        }
        return false;
    }
}
